package com.sc.VenusQueryBeta.ui.activites;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.hengjun.kingstar.sdk.MessageEvent;
import com.hengjun.kingstar.sdk.base.activity.BaseCompatActivity;
import com.hengjun.kingstar.sdk.base.fragment.BaseCompatFragment;
import com.hengjun.kingstar.sdk.helper.BottomNavigationViewHelper;
import com.hengjun.kingstar.sdk.rxbus.RxBus;
import com.hengjun.kingstar.sdk.utils.LogUtil;
import com.hengjun.kingstar.sdk.utils.LogUtils;
import com.hengjun.kingstar.sdk.utils.SpUtils;
import com.hengjun.kingstar.sdk.utils.ToastUtils;
import com.sc.VenusQueryBeta.R;
import com.sc.VenusQueryBeta.constant.Constants;
import com.sc.VenusQueryBeta.dae.JobSchedulerManager;
import com.sc.VenusQueryBeta.dae.ScreenManager;
import com.sc.VenusQueryBeta.dae.ScreenReceiverUtil;
import com.sc.VenusQueryBeta.global.MyApplication;
import com.sc.VenusQueryBeta.model.bean.login.UsersLoginBean;
import com.sc.VenusQueryBeta.model.sqlite.DataBaseDao;
import com.sc.VenusQueryBeta.model.sqlite.MsgListBean;
import com.sc.VenusQueryBeta.ui.fragments.home.HomeRootFragment;
import com.sc.VenusQueryBeta.ui.fragments.setting.SettingRootFragment;
import com.sc.VenusQueryBeta.ui.fragments.vip.VipRootFragment;
import com.sc.VenusQueryBeta.ui.fragments.vip.child.MsgFragment;
import com.sc.VenusQueryBeta.ui.fragments.vip.child.VipUserFragment;

import java.util.ArrayList;

import butterknife.BindView;
import cn.jpush.android.api.JPushInterface;
import io.reactivex.disposables.CompositeDisposable;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * 主页面
 */
public class MainActivity extends BaseCompatActivity implements BaseCompatFragment.OnBackToFirstListener {

    @BindView(R.id.fl_container)
    FrameLayout flContainer;
    @BindView(R.id.bottom_nv)
    BottomNavigationView bottomNv;

    //分别标识三个底部tab所在的fragment
    public static final int FIRST = 0;
    public static final int SECOND = 1;
    public static final int THIRD = 2;
    // 再点一次退出程序时间设置
    private static final long WAIT_TIME = 2000L;
    private long TOUCH_TIME = 0;
    private ScreenReceiverUtil mScreenListener;
    private ScreenManager mScreenManager;
    private JobSchedulerManager mJobManager;
    private UsersLoginBean loginBean;
    private CompositeDisposable compositeDisposable;

    private SupportFragment[] mFragments = new SupportFragment[3];//装根fragment的集合
    private boolean isNoti;
    private boolean inform;
    private int notificationId;
    private TextView textView;
    private AlertDialog alertDialog1;
    private AlertDialog.Builder dialogBuilder;
    private IntentFilter intentFilter;
    private RedPointReceiver localReceiver;
    private DataBaseDao dataBaseDao;
    private SQLiteDatabase rdb;
    private ArrayList<String> strings;
    private AlertDialog alertDialog;

    @Override
    protected void onResume() {
        super.onResume();
        loginBean = SpUtils.getObject(MyApplication.getContext(), UsersLoginBean.class);

        boolean isYiDi = SpUtils.getBoolean(MyApplication.app, Constants.DISTANCE_OUT, false);
        boolean isHtOut = SpUtils.getBoolean(MyApplication.app, Constants.ISHOUOTAIDAOQI, false);
        boolean isGUOQI = SpUtils.getBoolean(MyApplication.app, Constants.ISGUOQI, false);
        boolean isChange = SpUtils.getBoolean(MyApplication.app, Constants.ISCHANGEPWD, false);
        boolean isMsgBack = SpUtils.getBoolean(MyApplication.app, Constants.MSG_BACkGROUND, false);//后台收到通知 然后没有从通知栏打开 而是点击app的情况之下
        final int anInt = SpUtils.getInt(MyApplication.app, Constants.NOTIFICATION_ID, 0);
        LogUtils.e("id999====" + anInt);
//        isNoti = SpUtils.getBoolean(MyApplication.app, Constants.ISNOTIFICATION, false);
        inform = SpUtils.getBoolean(MyApplication.app, Constants.INFORM, false);
        //通知
        isNoti = SpUtils.getBoolean(MyApplication.app, Constants.ISNOTIFICATION, false);

        if (isMsgBack) {
            if (textView != null) {
                textView.setVisibility(View.VISIBLE);
                SpUtils.putBoolean(MyApplication.app, Constants.MSG_BACkGROUND, false);
            }
        }
        //点击通知栏回到登录页面(异地登录)
        if (inform) {
            LogUtil.e("inform");
            if (bottomNv.getSelectedItemId() == R.id.action_vip) {
                SpUtils.putBoolean(MyApplication.app, Constants.INFORM, false);
                start(VipRootFragment.newInstance(), 2);
            } else {
                bottomNv.setSelectedItemId(R.id.action_vip);
            }
        }
        //跳转信息页面
        if (loginBean != null) {
            if (isNoti) {
                if (bottomNv.getSelectedItemId() == R.id.action_vip) {
                    LogUtils.e("跳转信息页面");
                    SpUtils.putBoolean(MyApplication.app, Constants.ISNOTIFICATION, false);
                    start(VipUserFragment.newInstance(), 0);
                    start(MsgFragment.newInstance(), 0);
                } else {
                    LogUtils.e("跳转信息页面2");
//                SpUtils.putBoolean(MyApplication.app, Constants.ISNOTIFICATION, false);
//                start(VipUserFragment.newInstance(), 0);
//                start(MsgFragment.newInstance(), 0);
                    bottomNv.setSelectedItemId(R.id.action_vip);
//                start(MsgFragment.newInstance(), 2);

                }
            } else {
                LogUtils.e("跳转信息页面3");
            }
        }

        if (isHtOut) {//是后台到期
            start(VipRootFragment.newInstance());
            bottomNv.setSelectedItemId(R.id.action_vip);
            LogUtils.e("是后台到期，请重新登录");
            SpUtils.putBoolean(MyApplication.app, Constants.ISHOUOTAIDAOQI, false);

        } else {
//            ToastUtils.showToast("不是后台到期wq登录超时了，不请重新登录");
        }
        if (isGUOQI) {//是到期
            SpUtils.putBoolean(MyApplication.app, Constants.ISGUOQI, false);
//            start(VipRootFragment.newInstance());
            RxBus.getInstance().postSticky(new MessageEvent("123"));//通知vipuserfragment pop
            bottomNv.setSelectedItemId(R.id.action_vip);
            //图标
            alertDialog1 = new AlertDialog.Builder(this)
                    .setTitle("溫馨提示")//标题
                    .setMessage(R.string.login_out_date)//内容
                    .setIcon(R.drawable.img_logo_nav)//图标
                    .setNegativeButton("确定", null)
                    .create();
            alertDialog1.setCancelable(true);
            alertDialog1.setCanceledOnTouchOutside(false);
            alertDialog1.show();
        }
        if (isChange) {//是修改密码
            LogUtils.e("是修改密码，请重新登录");
            RxBus.getInstance().postSticky(new MessageEvent("123"));//通知vipuserfragment pop
            SpUtils.putBoolean(MyApplication.app, Constants.ISCHANGEPWD, false);
//            start(VipRootFragment.newInstance());
            bottomNv.setSelectedItemId(R.id.action_vip);
        }
        if (isYiDi) {//是异地登录
            if (loginBean!=null) {
                SpUtils.clear(MyApplication.app);

                SpUtils.putBoolean(MyApplication.app, Constants.OUT_DATE, true);
                SpUtils.putString(MyApplication.app, Constants.PHONE, loginBean.getId());
                SpUtils.putString(MyApplication.app, Constants.AREADECODE, loginBean.getAreaCode());
                LogUtil.e("id===="+SpUtils.getString(MyApplication.app, Constants.PHONE, "dad"));
            }
//            SpUtils.putBoolean(MyApplication.app, Constants.LOGINOUT, true);
//            String id = loginBean.getId();
//            String arCode = loginBean.getAreaCode();
//            SpUtils.clear(MyApplication.app);
//            SpUtils.putBoolean(MyApplication.app, Constants.OUT_DATE, true);
//            SpUtils.putString(MyApplication.app, Constants.PHONE, id);
//            SpUtils.putString(MyApplication.app, Constants.AREADECODE, arCode);
            if (bottomNv.getSelectedItemId() == R.id.action_vip) {
//                SpUtils.putBoolean(MyApplication.app, Constants.LOGINOUT, true);
                SpUtils.putBoolean(MyApplication.app, Constants.DISTANCE_OUT, false);
                RxBus.getInstance().postSticky(new MessageEvent("123"));//通知vipuserfragment pop
                start(VipRootFragment.newInstance());
//                SpUtils.putBoolean(MyApplication.app, Constants.LOGINOUT, true);
                LogUtil.e("是异地登录1");
                SpUtils.putBoolean(MyApplication.app, Constants.DISTANCE_OUT, false);
                SpUtils.putBoolean(MyApplication.app, Constants.OUT_DATE, true);
                SpUtils.putString(MyApplication.app, Constants.PHONE, loginBean.getId());
                SpUtils.putString(MyApplication.app, Constants.AREADECODE, loginBean.getAreaCode());
                bottomNv.setSelectedItemId(R.id.action_vip);

            } else {
                LogUtil.e("是异地登录2");
                RxBus.getInstance().postSticky(new MessageEvent("123"));//通知vipuserfragment pop
                bottomNv.setSelectedItemId(R.id.action_vip);
            }
            dialogBuilder = new AlertDialog.Builder(this);
            dialogBuilder.setTitle("VGS查数提示");
            dialogBuilder.setMessage("你的VGS查数账号正在异地登录！");
            dialogBuilder.setCancelable(false);
            dialogBuilder.setIcon(R.drawable.logo_login);
            dialogBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    RxBus.getInstance().postSticky(new MessageEvent("123"));//通知vipuserfragment pop
                    LogUtil.e("notificationId0=" + anInt);
                    SpUtils.clear(MyApplication.app);
                    JPushInterface.clearNotificationById(MyApplication.app, anInt);
                    SpUtils.putBoolean(MyApplication.app, Constants.DISTANCE_OUT, false);

                }
            });
            alertDialog = dialogBuilder.create();
//          alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            alertDialog.show();

            SpUtils.putBoolean(MyApplication.app, Constants.DISTANCE_OUT, false);
        }

        //红点处理
        isShowRed();

//        RxBus.getInstance().toObservable(PusReadNumEvent.class)
//                .subscribe(new Observer<PusReadNumEvent>() {
//                    @Override
//                    public void onSubscribe(Disposable d) {
//                        compositeDisposable.add(d);
//                    }
//
//                    @Override
//                    public void onNext(PusReadNumEvent setShoppingGoodsCount) {
//                        loginBean = SpUtils.getObject(MyApplication.getContext(), UsersLoginBean.class);
//
//                        if (loginBean != null) {
//                            int anInt1 = SpUtils.getInt(MyApplication.app, Constants.UNREAD_NUM, 0);
//                            ToastUtils.showToast("收到：" + anInt1);
//                            if (anInt1 == 0) {
//                                textView.setVisibility(View.GONE);
//                            } else {
//                                textView.setText("" + anInt1);
//                                textView.setVisibility(View.VISIBLE);
//                            }
//                        }
//
//
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//
//                    }
//
//                    @Override
//                    public void onComplete() {
//
//                    }
//                });
//        if (loginBean != null) {
//            boolean aBoolean = SpUtils.getBoolean(MyApplication.app, Constants.CLOSE_RED, false);
//            if (aBoolean) {
//                textView.setVisibility(View.GONE);
//            } else {
//                textView.setVisibility(View.VISIBLE);
//            }
//        } else {
//            textView.setVisibility(View.GONE);
//        }


        Intent intentx = new Intent("com.nyl.orderlybroadcast.VisibleReceiver");

        if (textView != null && textView.getVisibility() == View.VISIBLE) {//可见 通知vipuser可见
            //发送本地广播
            intentx.putExtra(Constants.VISIBLE, Constants.VISIBLE);
            LocalBroadcastManager.getInstance(this).sendBroadcastSync(intentx);
        } else {
            //发送本地广播
            intentx.putExtra(Constants.INVISIBLE, Constants.INVISIBLE);
            LocalBroadcastManager.getInstance(this).sendBroadcastSync(intentx);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (alertDialog1!=null) {
            alertDialog1.dismiss();
        }
        if (alertDialog!=null) {
            alertDialog.dismiss();
        }
    }

    private void isShowRed() {
        loginBean = SpUtils.getObject(MyApplication.getContext(), UsersLoginBean.class);
        if (loginBean != null && textView != null) {
            dataBaseDao = new DataBaseDao(MyApplication.app, loginBean.getId(), 11);
            rdb = dataBaseDao.databasehelper.getWritableDatabase();
//             //查询数据库看看是否存在未读消息
            java.util.List<MsgListBean> msgListBeans = dataBaseDao.searchAllRead();
            for (int i = 0; i < msgListBeans.size(); i++) {
                LogUtil.e("787878787////***==" + msgListBeans.get(i).getIsRead());
                if (msgListBeans.get(i).getIsRead().equals("0") && textView != null) {
                    textView.setVisibility(View.VISIBLE);
                }
            }
        } else {
            if (textView != null) {
                textView.setVisibility(View.GONE);
            }
        }
    }


    /**
     * 初始化布局
     *
     * @param savedInstanceState savedInstanceState
     */
    @Override
    protected void initView(Bundle savedInstanceState) {
        bottomNv.measure(0, 0);

        int g = bottomNv.getMeasuredHeight();
        LogUtil.e("bottomNv===========" + g);
        compositeDisposable = new CompositeDisposable();
        intentFilter = new IntentFilter();
        intentFilter.addAction("com.nyl.orderlybroadcast.AnotherBroadcastReceiver");
        localReceiver = new RedPointReceiver();
        //注册本地接收器
        LocalBroadcastManager.getInstance(this).registerReceiver(localReceiver, intentFilter);
        if (savedInstanceState == null) {
            //实例化每个fragment
            mFragments[FIRST] = HomeRootFragment.newInstance();
            mFragments[SECOND] = VipRootFragment.newInstance();
            mFragments[THIRD] = SettingRootFragment.newInstance();
            //加载fragment数据到帧布局
            loadMultipleRootFragment(R.id.fl_container, FIRST,
                    mFragments[FIRST],
                    mFragments[SECOND],
                    mFragments[THIRD]);
        } else {
            // 这里库已经做了Fragment恢复,所有不需要额外的处理了, 不会出现重叠问题..
            // 这里我们需要拿到mFragments的引用,也可以通过getSupportFragmentManager.getFragments()
            // 自行进行判断查找(效率更高些),用下面的方法查找更方便些
            mFragments[FIRST] = findFragment(HomeRootFragment.class);
            mFragments[SECOND] = findFragment(VipRootFragment.class);
            mFragments[THIRD] = findFragment(SettingRootFragment.class);
        }

        BottomNavigationViewHelper.disableShiftMode(bottomNv);//BottomNavigationView禁止3个item以上动画切换效果
        loginBean = SpUtils.getObject(MyApplication.getContext(), UsersLoginBean.class);

        bottomNv.setOnNavigationItemSelectedListener(new BottomNavigationView
                .OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {//导航栏监听事件
                switch (item.getItemId()) {
                    case R.id.action_home://首页
                        showHideFragment(mFragments[FIRST]);
//                        isShowRed();

                        break;
                    case R.id.action_vip://vip
                        loginBean = SpUtils.getObject(MyApplication.getContext(), UsersLoginBean.class);
                        if (SpUtils.getBoolean(MyApplication.app, Constants.LOGINOUT, false)) {//是点击退出登录后
                            LogUtil.e("是点击通知栏1");
                            SpUtils.putBoolean(MyApplication.app, Constants.LOGINOUT, false);
                            RxBus.getInstance().postSticky(new MessageEvent("123"));//通知vipuserfragment pop
                        }
                        showHideFragment(mFragments[SECOND]);

                        if (loginBean != null) {
                            if (SpUtils.getBoolean(MyApplication.app, Constants.ISNOTIFICATION, false)) {//是点击通知栏
                                LogUtil.e("是点击通知栏");
                                start(VipUserFragment.newInstance(), 0);
                                SpUtils.putBoolean(MyApplication.app, Constants.ISNOTIFICATION, false);
                                start(MsgFragment.newInstance(), 0);
                            } else {
                            }
                        }
//                        if (SpUtils.getBoolean(MyApplication.app, Constants.INFORM, false)) {//是点击通知栏异地
//                            SpUtils.putBoolean(MyApplication.app, Constants.INFORM, false);
//                            RxBus.getInstance().postSticky(new MessageEvent("123"));//通知vipuserfragment pop
//                            start(VipUserFragment.newInstance(),2);
//
//                        } else {
//                        }
                        break;
                    case R.id.action_setting://设置
                        showHideFragment(mFragments[THIRD]);
//                        isShowRed();

                        break;
                }
                return true;
            }
        });
        // 1. 注册锁屏广播监听器
//        mScreenListener = new ScreenReceiverUtil(this);
//        mScreenManager = ScreenManager.getScreenManagerInstance(this);
//        mScreenListener.setScreenReceiverListener(mScreenListenerer);
        // 2. 启动系统任务
//        mJobManager = JobSchedulerManager.getJobSchedulerInstance(this);
//        mJobManager.startJobScheduler();

//        startService(new Intent(MainActivity.this, DaemonService.class));

        //Settings.canDrawOverlays(this)  检查是否有悬浮窗权限
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            if(!Settings.canDrawOverlays(this)){
//                //没有悬浮窗权限,跳转申请
//                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
//                startActivity(intent);
//            }
//        }

        //获取整个的NavigationView
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNv.getChildAt(0);
        //这里就是获取所添加的每一个Tab(或者叫menu)，
        View tab = menuView.getChildAt(1);
        BottomNavigationItemView itemView = (BottomNavigationItemView) tab;
//加载我们的角标View，新创建的一个布局
        View badge = LayoutInflater.from(this).inflate(R.layout.im_badge, menuView, false);
//添加到Tab上
        itemView.addView(badge);
        textView = badge.findViewById(R.id.tv_msg_count);
//        textView.setText(String.valueOf(1));
//无消息时可以将它隐藏即可
//        textView.setVisibility(View.VISIBLE);

        if (textView != null && SpUtils.getBoolean(MyApplication.app, Constants.ACCEPT_NOTI_NOT_CLICK_MSG, false)) {//查看通知是否被消灭
            textView.setVisibility(View.VISIBLE);
        } else {
            textView.setVisibility(View.GONE);
        }


        Intent intentx = new Intent("com.nyl.orderlybroadcast.VisibleReceiver");

        if (textView != null && textView.getVisibility() == View.VISIBLE) {//可见 通知vipuser可见
            //发送本地广播
            intentx.putExtra(Constants.VISIBLE, Constants.VISIBLE);
            LocalBroadcastManager.getInstance(this).sendBroadcastSync(intentx);
        } else {
            //发送本地广播
            intentx.putExtra(Constants.INVISIBLE, Constants.INVISIBLE);
            LocalBroadcastManager.getInstance(this).sendBroadcastSync(intentx);
        }

    }


    private ScreenReceiverUtil.SreenStateListener mScreenListenerer = new ScreenReceiverUtil.SreenStateListener() {
        @Override
        public void onSreenOn() {
            // 亮屏，移除"1像素"
            loginBean = SpUtils.getObject(MyApplication.getContext(), UsersLoginBean.class);
            if (loginBean != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                    StatusBarUtils.setColor(MainActivity.this, getColor(R.color.theme_color));
                }
            }
//            mScreenManager.finishActivity();
        }

        @Override
        public void onSreenOff() {
            // 那么，我们就制造个"1像素"惨案
            Log.i("as", "1111111111111111111");
//            mScreenManager.startActivity();
        }

        @Override
        public void onUserPresent() {
            // 解锁，暂不用，保留
        }
    };

    /**
     * 写布局
     *
     * @return id
     */
    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    /**
     * 初始化数据
     */
    @Override
    protected void initData() {
        super.initData();
        //Logger.e("RxBus.get().register(this)");
        bottomNv.setItemIconTintList(null);//不设置选中后图片的颜色
        loginBean = SpUtils.getObject(MyApplication.getContext(), UsersLoginBean.class);
//        ToastUtils.showToast(StringUtils.FormatNumber("1836.026701"));

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //Logger.e("RxBus.get().unRegister(this)");
//        mScreenListener.stopScreenReceiverListener();
        if (compositeDisposable != null) {
            RxBus.rxBusUnbund(compositeDisposable);
        }

        if (alertDialog1 != null) {
            alertDialog1.dismiss();
        }

        if (localReceiver != null) {
            LocalBroadcastManager.getInstance(this).unregisterReceiver(localReceiver);
        }

        if (rdb != null) {
            rdb.close();
        }
    }

    @Override
    public void onBackPressedSupport() {
        if (getFragmentManager().getBackStackEntryCount() > 1) {
            //如果当前存在fragment>1，当前fragment出栈
            pop();
        } else {
//            ActivityCompat.finishAfterTransition(this);
//            pop();
            //如果已经到root fragment了，2秒内点击2次退出
            if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
                setIsTransAnim(false);
                finish();
            } else {
                TOUCH_TIME = System.currentTimeMillis();
                ToastUtils.showToast("再按一次退出");
            }
        }
    }

    @Override
    public void onBackToFirstFragment() {

    }

    //紅點更新
    public class RedPointReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //在这里写上相关的处理代码，一般来说，不要此添加过多的逻辑或者是进行任何的耗时操作
            //因为广播接收器中是不允许开启多线程的，过久的操作就会出现报错
            //因此广播接收器更多的是扮演一种打开程序其他组件的角色，比如创建一条状态栏通知，或者启动某个服务
            loginBean = SpUtils.getObject(MyApplication.getContext(), UsersLoginBean.class);
            if (loginBean != null) {
//                textView.setVisibility(View.VISIBLE);
//            }
                dataBaseDao = new DataBaseDao(MyApplication.app, loginBean.getId(), 11);
                rdb = dataBaseDao.databasehelper.getWritableDatabase();
                if (intent.getExtras() != null) {
                    if (intent.getExtras().getString(Constants.CLOSE_RED, "").equals("close")) {
                        java.util.List<MsgListBean> msgListBeans = dataBaseDao.searchAllRead();
//                        ToastUtils.showToast("紅點更新:"+msgListBeans.size());
                        for (int i = 0; i < msgListBeans.size(); i++) {
                            strings = new ArrayList<>();
                            strings.add(msgListBeans.get(i).getIsRead());
                            LogUtil.e("msgListBeans.get(i).getIsRead()=" + msgListBeans.get(i).getIsRead());
                            if (strings.get(0).equals("0") && textView != null) {
                                textView.setVisibility(View.VISIBLE);
                                break;
                            } else {
                                if (textView != null) {
                                    textView.setVisibility(View.GONE);
                                    SpUtils.putBoolean(MyApplication.app, Constants.ACCEPT_NOTI_NOT_CLICK_MSG, false);
                                }

                            }
                            strings.clear();
                        }
                        if (msgListBeans.size() == 0) {//清空阅读信息的时候
                            if (textView != null) {
                                textView.setVisibility(View.GONE);
                                SpUtils.putBoolean(MyApplication.app, Constants.ACCEPT_NOTI_NOT_CLICK_MSG, false);
                            }
                        }
                    } else {

                    }

                } else {
                    if (textView != null) {
                        textView.setVisibility(View.VISIBLE);
                        SpUtils.putBoolean(MyApplication.app, Constants.ACCEPT_NOTI_NOT_CLICK_MSG, true);
                    }
                }
                Intent intentx = new Intent("com.nyl.orderlybroadcast.VisibleReceiver");

                if (textView != null && textView.getVisibility() == View.VISIBLE) {//可见 通知vipuser可见
                    //发送本地广播
                    intentx.putExtra(Constants.VISIBLE, Constants.VISIBLE);
                    LocalBroadcastManager.getInstance(context).sendBroadcastSync(intentx);
                } else {
                    //发送本地广播
                    intentx.putExtra(Constants.INVISIBLE, Constants.INVISIBLE);
                    LocalBroadcastManager.getInstance(context).sendBroadcastSync(intentx);
                }
            } else {
                if (textView != null) {
                    textView.setVisibility(View.GONE);
                }
            }
        }
    }


}
